package com.cwt_taxi_xa;




import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cwt_taxi_xa.protocol.CardUtil;

public class ActivityWriteCard extends CWTBaseActivity{

	Button bt_cancel, bt_check, bt_write;
	EditText et1;
	TextView tv1;

	public static boolean isTest = false;

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==1001){
				String[] ss = (String[]) msg.obj;
				if(ss.length==4){
					String type = ss[0];
					String QYCODE = ss[1];
					String SJCODE = ss[2];
					String CARCODE = ss[3];
//					tv1.setText("类型（司机-1，管理者-2）："+type+"\n企业许可证："+QYCODE+"\n司机从业资格证："+SJCODE+"\n车牌号："+CARCODE);
					if(et1.getText().toString().endsWith(QYCODE)){
						tv1.setText("写卡成功："+QYCODE);
					}else{
						tv1.setText("读卡数据："+QYCODE);
					}
				}
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_writecard);
		bt_cancel = (Button) findViewById(R.id.button3);
		bt_check = (Button) findViewById(R.id.button1);
		bt_write = (Button) findViewById(R.id.button2);
		et1 = (EditText) findViewById(R.id.et_1);
		tv1 = (TextView) findViewById(R.id.textView1);
		bt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		bt_check.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				CardUtil.readCard(16*6);
//				CardUtil.setAutoRead(1);
			}
		});
		bt_write.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = "1-"+et1.getText()+"-0-0";
//				String str = "1-"+et1.getText()+"-"+et2.getText()+"-"+et3.getText();
				CardUtil.setCardData(CardUtil.parseInfo2CardData(str));
//				CardUtil.writeData(CardUtil.parseInfo2CardData(str));
			}
		});
	}



	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isTest = true;

		CardUtil.autoRead = false;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		isTest = false;

		CardUtil.autoRead = true;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		if(type==10086){
			handler.sendMessage(handler.obtainMessage(1001, obj));
		}
	}

}
